<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Category;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;

class CategoryController extends Controller
{
    //get.admin/category
    public function index(){
        $catagorys = Category::orderby('cate_order','asc')->get();

        $data = $this->getTree($catagorys);
        return view('admin.category.list')->with('data',$data);
    }

    public function getTree($data){
        $arr = array();
        foreach ($data as $k=>$v){
            if($v->cate_pid == 0){
                $arr[] = $data[$k];
                foreach ($data as $m=>$n){
                    if($n->cate_pid == $v->cate_id){
                        $arr[] =$data[$m];
                    }
                }
            }
        }
        return $arr;
    }

    public function changeOrder(){
        $input = Input::all();
        $cate = Category::find($input['cate_id']);
        $cate->cate_order = $input['cate_order'];
        $re = $cate->update();
        if($re){
            $data = array(
                'status' => 1,
                'msg' => '修改排序成功',
            );
        } else {
            $data = array(
                'status'=> 0,
                'msg' => '修改失败，请稍后再试',
            );
        }

        return $data;
    }


    //get.admin/
    public function  store(){

    }

}
